/**
 * 维护名片自动加人配置
 */
import { Service } from '@gnlab/utils'
import type { Services } from '@/services'
import { API } from '@/services/common/api'

// 添加好友的验证消息中可用的变量
export enum MessageVar {
  QywxName = 'WxWorkName', // 自己的微信昵称
  ContactName = 'ContactName', // 名片昵称
  SenderName = 'SenderName', // 名片发送者昵称
}

export interface PersonCardAutoApplyConfig {
  enable: boolean
  message: string
}
interface RawPersonCardAutoApplyConfig {
  Enable: boolean
  Verify: string | null
}
function formatPersonCardAutoApplyConfig(
  raw: RawPersonCardAutoApplyConfig,
): PersonCardAutoApplyConfig {
  return {
    enable: raw.Enable,
    message: raw.Verify ?? '',
  }
}
function getDefaultPersonCardAutoApplyConfig() {
  return {
    enable: false,
    message: '',
  }
}

export class PersonCardAutoApply extends Service<Services> {
  async get() {
    return API('Config/GetCardAutoApplyContact', {
      formatter(raw: RawPersonCardAutoApplyConfig | null) {
        return raw ? formatPersonCardAutoApplyConfig(raw) : getDefaultPersonCardAutoApplyConfig()
      },
    })
  }

  async save(config: PersonCardAutoApplyConfig) {
    return API('/Config/SaveCardAutoApplyContact', {
      data: {
        enable: config.enable,
        verify: config.message,
      },
      formatter() {},
    })
  }
}
